﻿using System;
using System.Text;
using System.Collections;
using RAP.Framework.Libary.DomTemplateBuilder.Element.ArrayListActions.Add;
using RAP.Framework.Libary.Utils;
using RAP.Framework.Libary.DomTemplateBuilder.Interface;

namespace RAP.Framework.Libary.DomTemplateBuilder.Handler.ArrayListActions.Add
{
	/// <summary>
	/// 添加一个值对象到字典项的Handler
	/// </summary>
	public class AddValueActionHandler : ArrayListAddActionHandler
	{
		public new AddValueAction Element
		{
			get
			{
				return base.Element as AddValueAction;
			}
			set
			{
				base.Element = value;
			}
		}

		public override void DoRender ( StringBuilder writer, object rootParamObject, object parentParamObject, ITemplateRenderContext context )
		{
			var sbValue = new StringBuilder ( );
			var single = Element.Single;
			base.DoRenderChildNodes ( sbValue, rootParamObject, parentParamObject, context );
			object value = sbValue.ToString ( ).Trim ( );
			var list = context[ ArrayListActionHandler.ARRAY_LIST_OBJECT ] as IList;
			var type = Element.ValueType;
			if ( type != TypeCode.Object )
			{
				value = value.ConvertValue ( type );
			}
			else
			{
				value = value.ToString ( ).Eval ( );
			}
			if ( Element.IgnoreEmpty && value.IsNullOrEmpty ( ) )
			{
				return;
			}
			if ( !single || ( single && !list.Contains ( value ) ) )
			{
				list.Add ( value );
			}
		}
	}
}
